//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet.UI
{
    class ScaleColorsToBrushConverter: IMultiValueConverter
    {
        
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var colors = (IEnumerable<SpreadsheetColor>) values[0];
            var colorScheme = StrictConverter.Convert<ColorScheme>(values[1]);
            var result = new LinearGradientBrush {EndPoint = new Point(0, 1)};
            var l = new List<SpreadsheetColor>(colors);
            if (l.Count == 0)
                return null;
            for (int i = 0; i < l.Count; i++)
            {
                result.GradientStops.Add(new System.Windows.Media.GradientStop(ColorToBrushConverter.GetBrush(l[i], colorScheme).Color,
                    ((double)(l.Count - 1 - i)) / (l.Count - 1)));
            }
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
